package cn.itcast.up.up_web.service;

import cn.itcast.up.up_web.bean.dto.ModelDto;
import cn.itcast.up.up_web.bean.dto.TagDto;
import cn.itcast.up.up_web.bean.dto.TagModel;
import cn.itcast.up.up_web.bean.po.ModelPo;
import cn.itcast.up.up_web.bean.po.TagPo;
import cn.itcast.up.up_web.repo.ModelRepository;
import cn.itcast.up.up_web.repo.TagRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagRepository tagRepository;

    @Autowired
    private ModelRepository modelRepository;
    @Autowired
    private EngineService engine;
    @Override
    public void saveAll(List<TagDto> list) {

        List<TagPo> poList = list.stream().map(this::convert).collect(Collectors.toList());
        TagPo tag1 = poList.get(0);
        TagPo tag2 = poList.get(1);
        TagPo tag3 = poList.get(2);
        tag1.setPid(-1L);
        TagPo tmpTag = tagRepository.findByLevelAndNameAndPid(tag1.getLevel(), tag1.getName(), tag1.getPid());
        if (tmpTag == null) {
            tmpTag = tagRepository.save(tag1);
        }
        tag2.setPid(tmpTag.getId());
        tmpTag = tagRepository.findByLevelAndNameAndPid(tag2.getLevel(), tag2.getName(), tag2.getPid());
        if (tmpTag == null) {
            tmpTag = tagRepository.save(tag2);
        }
        tag3.setPid(tmpTag.getId());
        tmpTag = tagRepository.findByLevelAndNameAndPid(tag3.getLevel(), tag3.getName(), tag3.getPid());
        if (tmpTag == null) {
            tmpTag = tagRepository.save(tag3);
        }
    }

    @Override
    public List<TagDto> findTagsByPid(Long pid) {
        List<TagPo> poList = tagRepository.findByPid(pid);
        return poList.stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public void saveTagModel(TagDto tag, ModelDto model) {
        TagPo tagPo = tagRepository.save(convert(tag));
        modelRepository.save(convert(model,tagPo.getId()));
    }

    @Override
    public List<TagModel> findAllTagModel(Long pid) {
        ArrayList<TagModel> list = new ArrayList<>();
//        根据pid找到一堆4级标签
        List<TagPo> tagPoList = tagRepository.findByPid(pid);
        for (TagPo tagPo : tagPoList) {
//            Long tagId = tagPo.getId();
//            ModelPo modelPo = modelRepository.findByTagId(tagId);
//            list.add(new TagModel(convert(tagPo), convert(modelPo)));
            ModelPo modelPo = null;
            if (tagPo.getLevel() == 4) {
                Long tagId = tagPo.getId();
                modelPo = modelRepository.findByTagId(tagId);
                list.add(new TagModel(convert(tagPo), convert(modelPo)));
            } else {
                list.add(new TagModel(convert(tagPo), null));
            }
        }
        return list;
    }

    @Override
    public void saveTag(TagDto tagDto) {
        tagRepository.save(convert(tagDto));
    }

    @Override
    public void updateModelState(Long id, Integer state) {
        ModelPo modelPo = modelRepository.findByTagId(id);
        if (state == ModelPo.STATE_ENABLE) {
            engine.startModel(convert(modelPo));
        }
        if (state == ModelPo.STATE_DISABLE) {
            engine.stopModel(convert(modelPo));
        }
        modelPo.setState(state);
        modelRepository.save(modelPo);
    }

    public ModelDto convert(ModelPo modelPo) {
        ModelDto modelDto = new ModelDto();
        modelDto.setId(modelPo.getId());
        modelDto.setName(modelPo.getName());
        modelDto.setMainClass(modelPo.getMainClass());
        modelDto.setPath(modelPo.getPath());
        modelDto.setArgs(modelPo.getArgs());
        modelDto.setState(modelPo.getState());
        modelDto.setSchedule(modelDto.parseDate(modelPo.getSchedule()));
        return modelDto;
    }

    public ModelPo convert(ModelDto modelDto,Long id) {
        ModelPo modelPo = new ModelPo();
        modelPo.setId(modelDto.getId());
        //设置4级ID为外部传入的ID
        modelPo.setTagId(id);
        modelPo.setName(modelDto.getName());
        modelPo.setMainClass(modelDto.getMainClass());
        modelPo.setPath(modelDto.getPath());
        modelPo.setSchedule(modelDto.getSchedule().toPattern());
        modelPo.setCtime(new Date());
        modelPo.setUtime(new Date());
        modelPo.setState(modelDto.getState());
        modelPo.setArgs(modelDto.getArgs());
        return modelPo;
    }
    public TagDto convert(TagPo po) {
        return new TagDto(po.getId(), po.getName(), po.getRule(), po.getLevel(), po.getPid(), po.getState());
    }

    private TagPo convert(TagDto dto) {
        TagPo po = new TagPo();
        po.setId(dto.getId());
        po.setName(dto.getName());
        po.setRule(dto.getRule());
        po.setLevel(dto.getLevel());
        po.setPid(dto.getPid());
        po.setState(dto.getState());
        return po;
    }
}
